package com.spzx.user.controller;

import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.user.service.ISmsService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * ClassName: SmsController
 * Package: com.spzx.user.controller
 * Description:
 *
 * @Author 咚咚小圆帽
 * @Create 2025/3/25 13:42
 * @Version 1.0
 */
@Slf4j
@Tag(name = "短信接口")
@RestController
@RequestMapping("/sms")
public class SmsController extends BaseController {

    @Autowired
    private ISmsService smsService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Operation(summary = "获取短信验证码")
    @GetMapping(value = "/sendCode/{phone}")
    public AjaxResult sendCode(
            @Parameter(name = "phone",
                    description = "手机",
                    required = true)
            @PathVariable
            String phone) throws Exception {
        //生成验证码
        int num = new Random().nextInt(10000);
        String code = new DecimalFormat("0000").format(num);

        //发送短信
        Map<String, Object> param = new HashMap<>();
        param.put("code", code);

        //为了省钱，可以将下面的代码屏蔽掉
//        smsService.sendCode(phone, "CST_ptdie100", param);
        //将短信中的验证码保存在redis中
        redisTemplate.opsForValue().set("phone:code:" + phone, code, 5, TimeUnit.MINUTES);

        return success();
    }

}
